package com.example.onlinetaxi.ui.activity.HomePage;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.HomeCallBack.IExchangeLoginMode;
import com.example.onlinetaxi.callback.HomeCallBack.IRegisterToLogin;
import com.example.onlinetaxi.ui.activity.BaseActivity;
import com.example.onlinetaxi.ui.activity.Driver.RegisterPage.DriverRegisterActivity;

public class HomeActivity extends BaseActivity implements View.OnClickListener {

    // 两个文本按钮
    private TextView loginTextView;
    private TextView registerTextView;
    // 司机注册跳转
    private TextView registerDriver;

    // 三个fragment
    private LoginFragment loginFragment = new LoginFragment();
    private RegisterFragment registerFragment = new RegisterFragment();
    private VerifyCodeLoginFragment verifyCodeLoginFragment = new VerifyCodeLoginFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        initView();
    }

    // 初始化控件
    private void initView() {
        // 初始化
        loginTextView = findViewById(R.id.login_tv);
        registerTextView = findViewById(R.id.register_tv);
        registerDriver = findViewById(R.id.register_driver);

        // 添加下划线
        registerDriver.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        // 设置默认颜色
        loginTextView.setTextColor(Color.BLACK);
        registerTextView.setTextColor(Color.GRAY);

        // 设置点击事件
        loginTextView.setOnClickListener(this);
        registerTextView.setOnClickListener(this);
        registerDriver.setOnClickListener(this);

        // 设置第一个出现的碎片
        replaceFragment(loginFragment);
        // 设置切换登录模式功能
        setExchangeMode();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_tv:
                loginTextView.setTextColor(Color.BLACK);
                registerTextView.setTextColor(Color.GRAY);
                // 设置切换登录模式功能
                replaceFragment(loginFragment);
                break;
            case R.id.register_tv:
                registerTextView.setTextColor(Color.BLACK);
                loginTextView.setTextColor(Color.GRAY);
                replaceFragment(registerFragment);
                break;
            case R.id.register_driver:
                // 跳转到司机注册界面
                Intent intent = new Intent(this, DriverRegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void setExchangeMode() {
        // 切换到验证码
        loginFragment.setExchangeLoginMode(new IExchangeLoginMode() {
            @Override
            public void exchangeLoginMode() {
                replaceFragment(verifyCodeLoginFragment);
            }
        });
        // 切换到密码
        verifyCodeLoginFragment.setExchangeLoginMode(new IExchangeLoginMode() {
            @Override
            public void exchangeLoginMode() {
                replaceFragment(loginFragment);
            }
        });
        // 注册成功切换到登录
        registerFragment.setIRegisterToLogin(new IRegisterToLogin() {
            @Override
            public void toLoginPage() {
                // 切换到登录
                replaceFragment(loginFragment);
                // 修改登录文本的颜色
                loginTextView.setTextColor(Color.BLACK);
                registerTextView.setTextColor(Color.GRAY);
            }
        });
    }

    // 进行碎片之间的切换
    private void replaceFragment(Fragment fragment) {
        // 获取碎片管理类
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启碎片事物
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 创建事务
        fragmentTransaction.replace(R.id.replaced_by_fragment, fragment);
        // 提交事务
        fragmentTransaction.commit();
    }
}